package com.sidianzhong.sdz.service.impl;
import com.github.pagehelper.PageHelper;
import com.sidianzhong.sdz.mapper.*;
import com.sidianzhong.sdz.model.*;
import com.sidianzhong.sdz.service.*;
import com.sidianzhong.sdz.utils.PageInfo;
import com.sidianzhong.sdz.utils.Tools;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
* Created by hxgqh on 2016/1/7.
*/
@Service
@Transactional
public class BorrowMoneyServiceImpl implements BorrowMoneyService{
    protected static final Logger LOG = LoggerFactory.getLogger(BorrowMoneyServiceImpl.class);
    // 将所有的modelMapper注入
        @Autowired
    private BorrowMoneyMapper borrowMoneyMapper;

    @Override
    public BorrowMoney create(BorrowMoney item) {
         borrowMoneyMapper.insert(item);
         return item;
    }

    @Override
    public int delete(Integer id) {
        return borrowMoneyMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int update(BorrowMoney item) {
        return borrowMoneyMapper.updateByPrimaryKeySelective(item);
    }

    @Override
    public BorrowMoney get(Integer id) {
        return borrowMoneyMapper.selectByPrimaryKey(id);
    }
    @Override
    public PageInfo<BorrowMoney> getListWithPaging(Integer pageNum, Integer pageSize,
                                                  String sortItem, String sortOrder,BorrowMoney item){
       BorrowMoneyExample  example = new BorrowMoneyExample();
       BorrowMoneyExample.Criteria criteria = example.createCriteria();
       if(item.getUserId() != null ){
            criteria.andUserIdEqualTo(item.getUserId());
        }
        if(item.getBorrowNum() != null ){
            criteria.andBorrowNumEqualTo(item.getBorrowNum());
        }
        if(item.getBorrowAmount() != null ){
            criteria.andBorrowAmountEqualTo(item.getBorrowAmount());
        }
        if(item.getCoinId() != null ){
            criteria.andCoinIdEqualTo(item.getCoinId());
        }
        if(item.getPeriods() != null ){
            criteria.andPeriodsEqualTo(item.getPeriods());
        }
        if(item.getInterest() != null ){
            criteria.andInterestEqualTo(item.getInterest());
        }
        if(item.getStatus() != null ){
            criteria.andStatusEqualTo(item.getStatus());
        }
        if(item.getCreateTime() != null ){
            criteria.andCreateTimeEqualTo(item.getCreateTime());
        }
        if(item.getLastUpdateTime() != null ){
            criteria.andLastUpdateTimeEqualTo(item.getLastUpdateTime());
        }
       example.setOrderByClause(Tools.humpToLine(sortItem)+" "+sortOrder);
       PageHelper.startPage(pageNum, pageSize);
       List<BorrowMoney> list = this.borrowMoneyMapper.selectByExample(example);
       PageInfo result = new PageInfo(list);
       return result;
    }

    @Override
    public List<BorrowMoney> getList(Integer pageNum, Integer pageSize,
                                                      String sortItem, String sortOrder,BorrowMoney item){
       BorrowMoneyExample  example = new BorrowMoneyExample();
       BorrowMoneyExample.Criteria criteria = example.createCriteria();
       if(item.getUserId() != null ){
            criteria.andUserIdEqualTo(item.getUserId());
        }
        if(item.getBorrowNum() != null ){
            criteria.andBorrowNumEqualTo(item.getBorrowNum());
        }
        if(item.getBorrowAmount() != null ){
            criteria.andBorrowAmountEqualTo(item.getBorrowAmount());
        }
        if(item.getCoinId() != null ){
            criteria.andCoinIdEqualTo(item.getCoinId());
        }
        if(item.getPeriods() != null ){
            criteria.andPeriodsEqualTo(item.getPeriods());
        }
        if(item.getInterest() != null ){
            criteria.andInterestEqualTo(item.getInterest());
        }
        if(item.getStatus() != null ){
            criteria.andStatusEqualTo(item.getStatus());
        }
        if(item.getCreateTime() != null ){
            criteria.andCreateTimeEqualTo(item.getCreateTime());
        }
        if(item.getLastUpdateTime() != null ){
            criteria.andLastUpdateTimeEqualTo(item.getLastUpdateTime());
        }
       example.setOrderByClause(Tools.humpToLine(sortItem)+" "+sortOrder);
       PageHelper.startPage(pageNum, pageSize);
       List<BorrowMoney> list = this.borrowMoneyMapper.selectByExample(example);
       return list;
    }

     @Override
     public PageInfo<JSONObject> getListWithObject(Integer pageNum, Integer pageSize,
                                                      String sortItem, String sortOrder,BorrowMoney item){
        BorrowMoneyExample  example = new BorrowMoneyExample();
        BorrowMoneyExample.Criteria criteria = example.createCriteria();
        if(item.getUserId() != null ){
            criteria.andUserIdEqualTo(item.getUserId());
        }
        if(item.getBorrowNum() != null ){
            criteria.andBorrowNumEqualTo(item.getBorrowNum());
        }
        if(item.getBorrowAmount() != null ){
            criteria.andBorrowAmountEqualTo(item.getBorrowAmount());
        }
        if(item.getCoinId() != null ){
            criteria.andCoinIdEqualTo(item.getCoinId());
        }
        if(item.getPeriods() != null ){
            criteria.andPeriodsEqualTo(item.getPeriods());
        }
        if(item.getInterest() != null ){
            criteria.andInterestEqualTo(item.getInterest());
        }
        if(item.getStatus() != null ){
            criteria.andStatusEqualTo(item.getStatus());
        }
        if(item.getCreateTime() != null ){
            criteria.andCreateTimeEqualTo(item.getCreateTime());
        }
        if(item.getLastUpdateTime() != null ){
            criteria.andLastUpdateTimeEqualTo(item.getLastUpdateTime());
        }
        example.setOrderByClause(Tools.humpToLine(sortItem)+" "+sortOrder);
        PageHelper.startPage(pageNum, pageSize);
        List<BorrowMoney> list = this.borrowMoneyMapper.selectByExample(example);
        List<JSONObject> lists =  list.stream().map(borrowMoney -> JSONObject.parseObject(JSONObject.toJSONString(borrowMoney))).collect(Collectors.toList());
        PageInfo result = new PageInfo(lists);
        return result;
        }

    @Override
    public long getCount(BorrowMoney item) {
        BorrowMoneyExample  example = new BorrowMoneyExample();
        BorrowMoneyExample.Criteria criteria = example.createCriteria();
        if(item.getUserId() != null ){
            criteria.andUserIdEqualTo(item.getUserId());
        }
        if(item.getBorrowNum() != null ){
            criteria.andBorrowNumEqualTo(item.getBorrowNum());
        }
        if(item.getBorrowAmount() != null ){
            criteria.andBorrowAmountEqualTo(item.getBorrowAmount());
        }
        if(item.getCoinId() != null ){
            criteria.andCoinIdEqualTo(item.getCoinId());
        }
        if(item.getPeriods() != null ){
            criteria.andPeriodsEqualTo(item.getPeriods());
        }
        if(item.getInterest() != null ){
            criteria.andInterestEqualTo(item.getInterest());
        }
        if(item.getStatus() != null ){
            criteria.andStatusEqualTo(item.getStatus());
        }
        if(item.getCreateTime() != null ){
            criteria.andCreateTimeEqualTo(item.getCreateTime());
        }
        if(item.getLastUpdateTime() != null ){
            criteria.andLastUpdateTimeEqualTo(item.getLastUpdateTime());
        }
        long count = borrowMoneyMapper.countByExample(example);
        return count;
    }

}
